function $(selector) {
	var tokens = [];
	tokens = selector.split(" ");
	var element;
	for (var i in tokens) {
		var token = tokens[i];
		if (token[0] == '#') {
			element = document.getElementById(token.substring(1 , token.length));
		}
		/*else if (token[0] == '.') {
			alert(element.tagName);
			for (var j in element.childNodes) {
				alert(element.childNodes[i].tagName);
			}
		}*/
	}
	return element;
}

function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

function ajaxGet(url , action) {
    var xhr = new XMLHttpRequest();
    var retval = "";
    xhr.open("GET",url,true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState==4 && xhr.status==200)
        {
            action(xhr.responseText);
        }
    }
    xhr.send();
}

function ajaxPost(url , data , action) {
    var xhr = new XMLHttpRequest();
    var retval = "";
    xhr.open("POST",url,true);
    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xhr.onreadystatechange = function() {
        if (xhr.readyState==4 && xhr.status==200)
        {
            action(xhr.responseText);
        }
    }
    xhr.send(data);
}

var timerHandle;

function everyTime(period , action) {
    action();
    timerHandle = setTimeout(function() {everyTime(period , action);}, period);
}

function stopTimer() {
    clearTimeout(timerHandle);
}

function validateField(name , rule) {
	if (rule['regex'] != null) {
		alert(rule['regex']);
	}
}

function validateField(form , index , attr) {
	var parent = form[index].parentNode.parentNode;
	var errorElm = $("#" + index + "-error");
	
	if (attr['regex'] != null) {
		if (!attr['regex'].test(form[index].value)) {
			if (errorElm == null) {
				var element = document.createElement('span');
				element.setAttribute('class' , 'error');
				element.setAttribute('id' , index + '-error');
				element.innerHTML = attr['message'];
				parent.appendChild(element);
			}
			return false;
		}
		else {
			if (errorElm != null)
				parent.removeChild($("#" + index + "-error"));
			return true;
		}
	}
	
	if (attr['match'] != null) {
		var elementMatched = form[attr['match']] ? form[attr['match']] : null;
		if (elementMatched.value != form[index].value) {
			if (errorElm == null) {
				var element = document.createElement('span');
				element.setAttribute('class' , 'error');
				element.setAttribute('id' , index + '-error');
				element.innerHTML = attr['message'];
				parent.appendChild(element);
			}
			return false;
		}
		else {
			if (errorElm != null)
				parent.removeChild($("#" + index + "-error"));
			return true;
		}
	}
}

function setValidator(form , index , attr) {
	return function() {
		validateField(form , index , attr);
	}
}

function setFormValidator(form , attrs) {
	return function() {
		var retval = true;
		for (var i in attrs) {
			if (!validateField(form , i , attrs[i]))
				retval = false;
		}
		return retval;
	}
}

function validate(name , attr) {
	var form = document.forms[name];
	
	for (var i in attr) {
		form[i].onchange = setValidator(form , i , attr[i]);
	}
	
	form.onsubmit = setFormValidator(form , attr);
}